package com.yikaiye.android.yikaiye.ui.activity.mine;

import androidx.annotation.NonNull;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.databinding.ActivityMyCompanyBinding;
import com.yikaiye.android.yikaiye.ui.activity.order.viewmodel.MyCompanyViewModel;
import com.yikaiye.android.yikaiye.ui.adapter.MyCompanyAdapter;
import com.yikaiye.android.yikaiye.utils.JumpIntent;

/**
 * 我的企业
 */
public class MyCompanyActivity extends BaseActivity<ActivityMyCompanyBinding, MyCompanyViewModel> {

    private MyCompanyAdapter companyAdapter;
    private int page = 1;

    @Override
    protected MyCompanyViewModel bindModel() {
        return getViewModel(this, MyCompanyViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        binding.smartRefreshLayout.setEnableOverScrollDrag(true);
        companyAdapter = new MyCompanyAdapter(context);
        binding.recyclerView.setAdapter(companyAdapter);
        viewModel.getMyCompanyList(page, null);
        companyAdapter.setEmptyViewEnable(true);
        companyAdapter.setEmptyViewLayout(context, R.layout.layout_empty_list);
    }

    @Override
    protected void initClick() {
        viewModel.onDelayClick(binding.ivBack, o -> finish());
        viewModel.onDelayClick(binding.searchCompanyLl, o -> JumpIntent.jump(this, SearchCompanyActivity.class));
        binding.smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                viewModel.getMyCompanyList(page, null);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                viewModel.getMyCompanyList(page, null);
            }
        });

        viewModel.companyRecordsModelMutableLiveData.observe(this, companyModels -> {
            if (companyModels != null) {
                if (page == 1) {
                    companyAdapter.submitList(companyModels.getRecords());
                    binding.smartRefreshLayout.finishRefresh();
                } else {
                    companyAdapter.addAll(companyModels.getRecords());
                    binding.smartRefreshLayout.finishLoadMore();
                }
            }
        });
    }
}